package com.ai3d.rfid.base.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View

/**
 * @ProjectName : Kolb
 * @Author : weijinsong
 * @Time : 2023/1/4 11:26
 * @Description : 文件描述
 */
abstract class BaseDialog : Dialog {
    private lateinit var mRootView: View
    private var mAnimId = 0

    constructor(context: Context) : super(context)
    constructor(context: Context, themeResId: Int) : super(context, themeResId)
    protected constructor(
        context: Context,
        cancelable: Boolean,
        cancelListener: DialogInterface.OnCancelListener?
    ) : super(context, cancelable, cancelListener)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val layoutInflater = LayoutInflater.from(context)
        mRootView = layoutInflater.inflate(getLayoutId(), null)
        setContentView(mRootView)
        initWindowParams()
        initView()
        initEvent()
        initData()
    }

    private fun initWindowParams() {
        setCanceledOnTouchOutside(false)
        val window = window
        mAnimId = getAnimId()
        if (-1 != mAnimId) {
            window!!.setWindowAnimations(mAnimId)
        }
        val layoutParams = window!!.attributes
        if (-1 != getWidthResId()) {
            layoutParams.width = context.resources.getDimension(getWidthResId()).toInt()
        }
        if (-1 != getHeightResId()) {
            layoutParams.height = context.resources.getDimension(getHeightResId()).toInt()
        }
        if (-1 != getWidth() && -1 != getHeight()) {
            layoutParams.width = getWidth()
            layoutParams.height = getHeight()
        }
    }


    protected abstract fun initView()
    protected abstract fun initEvent()
    protected abstract fun initData()
    protected abstract fun getLayoutId(): Int
    protected abstract fun getAnimId(): Int
    protected open fun getWidthResId(): Int {
        return -1
    }

    protected open fun getHeightResId(): Int {
        return -1
    }

    protected open fun getWidth(): Int {
        return -1
    }

    protected open fun getHeight(): Int {
        return -1
    }
}